<!--
 * @Author: gao_fm@hdec.com
 * @Date: 2020-03-09 09:42:23
 * @LastEditors: zhong_m
 * @LastEditTime: 2021-12-20 14:17:04
 * @Description: 岗位管理
 -->
<template>
  <fks-dialog
    :visible.sync="show"
    :append-to-body="true"
    width="960px"
    destroy-on-close
    @close="close"
  >
    <div slot="title">
      <i
        v-if="title == LANG.ADD"
        class="fks-icon-plus"
      />
      <i
        v-if="title == LANG.EDIT"
        class="fks-icon-edit"
      />
      {{ title }}
    </div>
    <fks-form
      v-if="show"
      ref="form"
      :model="form"
      label-width="85px"
      :rules="rules"
    >
      <fks-form-item
        :span="12"
        lead
        :label="LANG.POSITION_NAME"
        prop="postName"
      >
        <fks-input
          v-model="form.postName"
          auto-complete="off"
          maxlength="50"
          show-word-limit
        />
      </fks-form-item>
      <fks-form-item
        :span="12"
        :label="LANG.POSITION_CODE"
        prop="postCode"
      >
        <fks-input
          v-model="form.postCode"
          auto-complete="off"
          maxlength="50"
          show-word-limit
        />
      </fks-form-item>
      <fks-form-item
        :label="LANG.REMARK"
        prop="remark"
      >
        <fks-input
          v-model="form.remark"
          auto-complete="off"
          maxlength="50"
          show-word-limit
        />
      </fks-form-item>
    </fks-form>
    <template slot="footer">
      <fks-button
        type="primary"
        :loading="postLoading"
        icon="fks-icon-check"
        @click="save"
      >
        <span style="margin-left: 4px;">{{ LANG.SAVE }}</span>
      </fks-button>
    </template>
  </fks-dialog>
</template>

<script type="text/ecmascript-6">
import * as Auth from '../api'
import * as types from '@/store/Getter/getterTypes.js'
export default {
  components: {},
  props: {
    data: {
      default() {
        return {
          postCode: '',
          remark: ''
        }
      }
    },
    visible: {
      type: Boolean,
      default: false
    },
    postType: {
      type: String,
      default: 'add'
    }
  },
  data() {
    return {
      show: this.visible,
      postLoading: false,
      form: Object.assign({}, this.data)
    }
  },
  computed: {
    rules() {
      return {
        postName: [
          {
            required: true,
            message: this.LANG.POSITION_NAME_NOT_EMPTY,
            trigger: 'change'
          }
        ],
        postCode: [
          {
            required: true,
            message: this.LANG.POSITION_CODE_NOT_EMPTY,
            trigger: 'change'
          }
        ]
      }
    },
    title() {
      switch (this.postType) {
        case 'add':
          return this.LANG.ADD
        default:
          return this.LANG.EDIT
      }
    },
    type() {
      return this.$store.state.portal.type
    },
    portalId() {
      return this.$store.state.portal.id
    }
  },
  watch: {
    data() {
      this.form = Object.assign({}, this.data)
    },
    visible() {
      this.show = this.visible
    }
  },
  methods: {
    //岗位新增表单提交
    save() {
      this.postLoading = true
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.title == this.LANG.EDIT) {
            Auth.updatePost(
              Object.assign(
                this.form,
                { portalId: this.portalId },
                { type: this.type }
              )
            )
              .then((res) => {
                if (res.status) {
                  this.$message.success(this.LANG.EDIT_SUCCESS)
                  this.$parent.getPost()
                  this.close()
                }
                this.postLoading = false
              })
              .catch(() => {
                this.postLoading = false
              })
          } else {
            Auth.addPost(
              Object.assign(
                this.form,
                { portalId: this.portalId },
                { type: this.type }
              )
            )
              .then((res) => {
                if (res.status) {
                  this.$message.success(this.LANG.ADD_SUCCESS)
                  this.$parent.getPost()
                  this.close()
                }
                this.postLoading = false
              })
              .catch(() => {
                this.postLoading = false
              })
          }
        } else {
          this.postLoading = false
        }
      })
    },
    //关闭弹窗
    close() {
      this.show = false
      this.$refs.form.clearValidate()
      this.$emit('update:visible', this.show)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
